﻿@model ExcellOnServices.Models.Order

@{
    ViewBag.Title = "Details";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    decimal totalMoney = 0;
    int dateCount = Model.EndDateTime.Subtract(Model.BeginDateTime).Days;
}
@using (Html.BeginForm("EditStatus", "Orders", new { id = Model.Id }, FormMethod.Post, new { @class = "form-inline" }))
{
    <div class="panel panel-primary">
        <div class="panel-heading">Order Details</div>
        <div class="panel-body">
            <p>
                @Html.LabelFor(model => model.OrderItems.FirstOrDefault().Product.Accounts_Company.BrandName, "Company", new { @class = "control-label col-md-2" })
                @Html.DisplayFor(model => model.OrderItems.FirstOrDefault().Product.Accounts_Company.BrandName)
            </p>
            <p>
                @Html.LabelFor(model => model.BeginDateTime, "Begin Date", new { @class = "control-label col-md-2" })
                @Html.DisplayFor(model => model.BeginDateTime)
            </p>
            <p>
                @Html.LabelFor(model => model.EndDateTime, "End Date", new { @class = "control-label col-md-2" })
                @Html.DisplayFor(model => model.EndDateTime)
            </p>
            <p>
                @Html.LabelFor(model => model.Payment, new { @class = "control-label col-md-2" })
                @switch ((int)@Model.Payment)
                {
                    case 0:
                        @Html.Label("payment", "Cash", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                    case 1:
                    @Html.Label("payment", "Credit Card", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                }
            </p>
            <p>
                @Html.LabelFor(model => model.Status, new { @class = "control-label col-md-2" })
                @switch ((int)@Model.Status)
                {
                    case 0:
                        @Html.Label("status", "Pending", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                    case 1:
                    @Html.Label("status", "Canceled", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                    case 2:
                    @Html.Label("status", "Confirmed", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                    case 3:
                    @Html.Label("status", "Activated", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                    case 4:
                    @Html.Label("status", "Expired", new { @class = "control-label", style = "font-weight: normal;" })
                        break;
                }
            </p>
        </div>
        <table class="table table-striped table-hover">
            <tr>
                <th>
                    Image
                </th>
                <th>
                    Category
                </th>
                <th>
                    Name
                </th>
                <th>
                    Service
                </th>
                <th>
                    Service Price
                </th>
                @if (Model.Status == ExcellOnServices.Models.Order.StatusOrder.Pending)
                {
                    <th>
                        Publish
                    </th>
                }
            </tr>

            @foreach (var item in Model.OrderItems)
            {
                <tr>
                    <td>
                        <img src="@item.Product.ImageUrl" style="width:50px;" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Product.Category.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Product.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Service.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ServicePrice)
                        @Html.Hidden("totalMoney", totalMoney += item.ServicePrice * dateCount)
                    </td>
                    @if (Model.Status == ExcellOnServices.Models.Order.StatusOrder.Pending)
                    {
                        <td>
                            <input type="checkbox" class="ckPublish" name="ckPublish" value="@item.Product.Id" checked="checked" prodid="@item.Product.Id" sprice="@item.ServicePrice" orderid="@item.Order_Id">
                        </td>
                    }
                </tr>
            }
            <tr class="danger">
                <th colspan="4">
                    Total of money:
                </th>
                <td id="totalPrice" totalprice=@totalMoney datecount=@dateCount>
                    @String.Format("{0:C}", @totalMoney)
                </td>
                <td></td>
            </tr>
        </table>
        <div class="panel-footer">
            @if (Model.Status == ExcellOnServices.Models.Order.StatusOrder.Pending)
            {
                <button type="submit" class="btn btn-primary btn-sm">Confirm</button>
            }
            @if (Model.Status == ExcellOnServices.Models.Order.StatusOrder.Confirmed)
            {
                <button type="submit" class="btn btn-primary btn-sm">Activate</button>
            }
            @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-primary btn-sm" })
        </div>
    </div>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script src="~/Content/scripts/jquery.price_format.1.8.min.js"></script>
    <script>
        $(function () {
            var publishProductInOrder = function () {
                totalPrice = parseFloat($("#totalPrice").attr("totalprice"));
                dateCount = parseInt($("#totalPrice").attr("dateCount"));
                servicePrice = parseFloat($(this).attr("sprice"));

                if ($(this).prop('checked')) {
                    totalPrice += servicePrice * dateCount;

                } else {
                    totalPrice -= servicePrice * dateCount;
                }
                $("#totalPrice").attr("totalprice", totalPrice);
                $("#totalPrice").html("$" + totalPrice.toFixed(2).replace(/./g, function (c, i, a) {
                    return i && c !== "." && !((a.length - i) % 3) ? ',' + c : c;
                }));
            }
            $(document).delegate(".ckPublish", 'change', publishProductInOrder);
        });
    </script>
}
